package cart

import (
	"gin/app/http/global"
	"gin/app/http/model/carts"
	"gin/app/http/response"
	"gin/app/http/struct/cart"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

// 添加商品到购物车
func AddCart(c *gin.Context) {
	DecryptedData, _ := c.Get("DecryptedData")
	var params cart.AddCartParams
	if err := binding.JSON.BindBody([]byte(DecryptedData.(string)), &params); err != nil {
		response.Fail(c, "参数不正确！", gin.H{
			"error": err.Error(),
		})
		return
	}

	db := global.DB
	user_id := c.GetInt("user_id")
	insertData := carts.Cart{
		UserId:    user_id,
		ProductId: params.ProductId,
		Quantity:  params.Quantity,
	}
	var count int64
	countSql := "select quantity from zlg_cart where user_id=? and product_id=?"
	db.Raw(countSql, user_id, params.ProductId).Scan(&count)
	if count == 0 {
		db.Create(&insertData)
	} else {
		sql := "UPDATE zlg_cart SET quantity = ? WHERE user_id=? and product_id=?"
		num := count + 1
		result := db.Exec(sql, num, user_id, params.ProductId)
		if result.Error != nil {
			c.JSON(500, gin.H{"error": result.Error.Error()})
			return
		}
	}
	response.Success(c, "请求成功", gin.H{})
}

type CartList struct {
	Id        int    `json:"id"`
	UserId    int    `json:"user_id"`
	ProductId int    `json:"product_id"`
	Quantity  int    `json:"quantity"`
	Name      string `json:"name"`
	Price     string `json:"price"`
	Image     string `json:"image"`
}

// 购物车列表
func GetCartList(c *gin.Context) {

	DecryptedData, _ := c.Get("DecryptedData")
	var params cart.GetCartListParams
	if err := binding.JSON.BindBody([]byte(DecryptedData.(string)), &params); err != nil {
		response.Fail(c, "参数不正确！", gin.H{
			"error": err.Error(),
		})
		return
	}
	user_id := c.GetInt("user_id")
	db := global.DB
	// 执行原生查询
	var cartLists []CartList
	sql := "SELECT c.cart_id as id,c.product_id,c.user_id,c.quantity,p.name,p.price,p.image " +
		" FROM zlg_cart as c " +
		"LEFT JOIN zlg_product as p " +
		" ON c.product_id = p.id " +
		"WHERE c.user_id = ? "
	result := db.Raw(sql, user_id).Scan(&cartLists)
	if result.Error != nil {
		response.Fail(c, "请求失败", gin.H{})
	} else {
		response.Success(c, "请求成功", gin.H{"data": cartLists})
	}
}

type DeleteCartParams struct {
	Cart_id int `json:"cart_id"`
}

// 删除购物车
func Remove(c *gin.Context) {
	DecryptedData, _ := c.Get("DecryptedData")
	var params carts.Cart
	if err := binding.JSON.BindBody([]byte(DecryptedData.(string)), &params); err != nil {
		response.Fail(c, "参数不正确！", gin.H{
			"error": err.Error(),
		})
		return
	}
	db := global.DB
	// 执行原生查询
	var cartLists carts.Cart
	result := db.Delete(&cartLists, params.CartId)
	if result.Error != nil {
		response.Fail(c, "请求失败", gin.H{})
	} else {
		response.Success(c, "请求成功", gin.H{})
	}
}

type updateParams struct {
	CartId int    `json:"cart_id"`
	Type   string `json:"type"`
}

// 更新购物车
func Update(c *gin.Context) {
	DecryptedData, _ := c.Get("DecryptedData")
	var params updateParams
	if err := binding.JSON.BindBody([]byte(DecryptedData.(string)), &params); err != nil {
		response.Fail(c, "参数不正确！", gin.H{
			"error": err.Error(),
		})
		return
	}
	db := global.DB
	// 执行原生查询
	var cartLists carts.Cart
	result := db.Where("cart_id = ?", params.CartId).First(&cartLists)
	if result.Error != nil {
		panic(result.Error)
	}
	if params.Type == "increate" {
		cartLists.Quantity += 1
	}
	if params.Type == "reduce" {
		cartLists.Quantity -= 1
	}
	result2 := db.Where("cart_id = ?", params.CartId).Save(&cartLists) // 保存更新
	if result2.Error != nil {
		response.Fail(c, "请求失败", gin.H{})
	} else {
		response.Success(c, "请求成功", gin.H{})
	}
}
